package com.sch.wanandroid.ui.girls

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.coder.zzq.smartshow.toast.SmartToast
import com.sch.wanandroid.R
import com.sch.wanandroid.adapter.GirlsAdapter
import com.sch.wanandroid.base.BaseActivity
import com.sch.wanandroid.entity.GirlEntity
import com.sch.wanandroid.ui.girls.info.GirlsInfoActivity
import kotlinx.android.synthetic.main.activity_girls.*

/**
 * Created by Sch.
 * Date: 2020/12/16
 * description:
 */
class GirlsActivity : BaseActivity() {
    private lateinit var girlsViewModel: GirlsViewModel
    private val girlsAdapter by lazy { GirlsAdapter() }
    private var pageNum = 1
    private val girlsList by lazy { ArrayList<GirlEntity>() }

    override fun getLayoutId(): Int {
        return R.layout.activity_girls
    }

    override fun init(savedInstanceState: Bundle?) {
        initTitle("轻松一下")
        girlsViewModel = ViewModelProvider(this).get(GirlsViewModel::class.java)
        //瀑布流
        val staggeredGridLayoutManager =
            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        recyclerView.layoutManager =
            staggeredGridLayoutManager
        recyclerView.adapter = girlsAdapter
        //保证每次滑动一个完整的item，
//        val pagerSnapHelper = PagerSnapHelper()
//        pagerSnapHelper.attachToRecyclerView(recyclerView)

        recyclerView.overScrollMode = RecyclerView.OVER_SCROLL_NEVER//取消滑动到顶部边界越界效果
        loadingTip.loading()
        loadData()
        initListener()
        girlsViewModel.girlsLiveData.observe(this) {
            setData(it)
        }
        girlsViewModel.errorMsg.observe(this) {
            onError(it)
        }
    }

    private fun initListener() {

        // 设置无网络时重新加载点击事件
        loadingTip.setReloadListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                loadingTip.loading()
                loadData()
            }
        })
        //下拉监听
        smartRefresh.setOnRefreshListener {
            loadData()
        }
        smartRefresh.setOnLoadMoreListener {
            pageNum++
            girlsViewModel.getRandomGirls()
        }
        girlsAdapter.setOnItemClickListener { adapter, view, position ->
            intent(Bundle().apply {
                putString("id", girlsList[position]._id)
            }, GirlsInfoActivity::class.java, false)
        }

    }

    private fun setData(list: List<GirlEntity>) {
        dismissRefresh()
        if (list.isNotEmpty()) {
            girlsList.addAll(list)
            girlsAdapter.setList(girlsList)
        } else {
            if (girlsList.size == 0) loadingTip.showEmpty()
            else SmartToast.info("没有更多数据了")
        }
    }

    private fun loadData() {
        girlsList.clear()
        girlsAdapter.setList(girlsList)
        pageNum = 1
        girlsViewModel.getRandomGirls()

    }

    fun onError(ex: String?) {
        //请求失败将page -1
        if (pageNum > 0) pageNum--
        dismissRefresh()
        if (girlsList.size == 0) {
            loadingTip.showInternetError()
        }
        SmartToast.error(ex)
    }

    /**
     * 隐藏刷新加载
     */
    private fun dismissRefresh() {
        loadingTip.dismiss()
        if (smartRefresh.state.isOpening) {
            smartRefresh.finishLoadMore()
            smartRefresh.finishRefresh()
        }
    }
}